Problem statement:

You have a number N,  You have to write a function which generates all the  N-Bit Gray Code Sequences.

#include <bits/stdc++.h>
using namespace std;

string get_string(char c)
{
	string g(1,c);
	return g;
}
void generateCode(int n)
{
	if(n<=0)
		{
		cout<<"invalid input\n";
		return;
		}

	vector<string> b,temp;
	b.push_back("0");
	b.push_back("1");

	for(int i=0;i<n-1;i++)
	{
		for(int j=0;j<b.size();j++)
			{
			temp.push_back(get_string('0')+b[j]);
			}
		for(int j=b.size()-1;j>=0;j--)
			{
			temp.push_back(get_string('1')+b[j]);
			}
		b=temp;
		temp.clear();
	}

			for(int i=0;i<b.size();i++)
			cout<<b[i]<<"\n";
}
int main()
	{
	int n;
	cout<<"Enter i/p n:\n";
	cin>>n;
	cout<<"Printing gray codes for given i/p "<<n<<" bit\n";
	generateCode(n);
	return 0;
	}

Algorithm to generate N Bit Gray Code list
1) Start
2) Now Let '(n-1)' be the length of the gray code lists that has m elements, [(m=2^(n-1))]
3) In this step let us create a list called 'LIST A' by adding the prefix element '0' to all the 'm elements' in the 'LIST A'
4) In this step let us create a list called 'LIST B' by adding the prefix element '1' to all the 'm elements' in the 'LIST B'
5) Now the newest 'LIST C' for n-bit gray code is = 'LIST A + reverse(LIST C)'
6) Now the newest 'LIST C'size is [2m (2^n)]
7) Stop

NOTE:
For all the n bit gray code list, we start with the list of length 1 and the elements are only '0' and '1'.

Example for the algorithm:
    
    For n=1

    List: [ 0, 1 ]

    For n=2

    Add prefix '0' to create list A
    List A= [ 00, 01 ]

    Add prefix '1' to create list B
    List B= [ 10, 11 ]

    New list C = list A + reverse(list B)
    =[ 00, 01, 11, 10 ] //for n=2

    For n=3

    Add prefix '0' to create list D
    List D= 000, 001, 011, 010

    Add prefix '1' to create list E
    List E= 100, 101, 111, 110

    New list F=list D + reverse(list E)
    =[000, 001, 011, 010, 110, 111, 101, 100 ]//for n=3

    For n=4
    Add prefix '0' to create list G
    List D= 0000, 0001, 0011, 0010 ,0110 ,0111 , 0101, 0100

    Add prefix '1' to create list H
    List E= 1100, 1101, 1111, 1110, 1010,,1011, 1001, 1000 

    New list I=list G + reverse(list H)
    =[0000, 0001, 0011, 0010, 0110, 0111, 0101,0100,1100,1101,1111,1110,1010,1011,1001,1000 ] 

Definition
The reflected binary code or Gray code is an ordering of the binary numeral system such that two successive values differ in only one bit (binary digit).
 This cyclic variable code that means every transition from one value to the next value involves only one bit change.                 


